package com.ruoyi.industryfrontend.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industryfrontend.domain.ProjectPark;
import com.ruoyi.industryfrontend.domain.SpaceIndustry;
import com.ruoyi.industryfrontend.mapper.DistrictMapper;
import com.ruoyi.industryfrontend.mapper.ProjectParkMapper;
import com.ruoyi.industryfrontend.mapper.SpaceCollectMapper;
import com.ruoyi.industryfrontend.mapper.SpaceIndustryMapper;
import com.ruoyi.industryfrontend.service.IProjectParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目-园区Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-05
 */
@Service
public class ProjectParkServiceImpl implements IProjectParkService
{
    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private DistrictMapper districtMapper;
    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;
    /**
     * 查询项目-园区
     *
     * @param id 项目-园区主键
     * @return 项目-园区
     */
    @Override
    public ProjectPark selectProjectParkById(Long id)
    {
        return projectParkMapper.selectProjectParkById(id);
    }

    @Override
    public Map<String, Object> selectProjectParkByNumber(String spaceId) {
        ProjectPark projectPark = projectParkMapper.selectProjectParkByNumber(spaceId);
        List<String> list = spaceCollectMapper.selectSpaceCollectByUserId(SecurityUtils.getUserId(),spaceId);
        if (list!=null&&list.size()!=0){
            projectPark.setCollectState(1L);
        }else {
            projectPark.setCollectState(0L);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("park",projectPark);
        map.put("space",projectPark);
        return map;
    }

    /**
     * 查询项目-园区列表
     *
     * @param projectPark 项目-园区
     * @return 项目-园区
     */
    @Override
    public List<ProjectPark> selectProjectParkList(ProjectPark projectPark)
    {
        //所属城市
//        List<Long> districtIds = new ArrayList<Long>();
//        if (projectPark.getCityId()!=null){
//            districtIds.add(projectPark.getCityId());
//            projectPark.setCityIds(districtIds);
//        }else if (projectPark.getCityId()==null && projectPark.getProvinceId() != null){
//            //查询省份下的城市
//            districtIds.addAll(districtMapper.selectDistrictIdList(projectPark.getProvinceId()));
//            districtIds.add(projectPark.getProvinceId());
//            projectPark.setCityIds(districtIds);
//        }
        projectPark.setSupplyUserId(SecurityUtils.getUserId());
        return projectParkMapper.selectProjectParkList(projectPark);
    }

    /**
     * 新增项目-园区
     *
     * @param projectPark 项目-园区
     * @return 结果
     */
    @Override
    public int insertProjectPark(ProjectPark projectPark)
    {
        projectPark.setCreateTime(DateUtils.getNowDate());
        projectPark.setApprovalState(0L);//审核：通过
        projectPark.setSupplyUserId(SecurityUtils.getUserId());//发布人
        projectPark.setSerialNumber(IdUtils.fastSimpleUUID());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectPark, spaceIndustry);
        spaceIndustry.setCreateBy(SecurityUtils.getUsername());
        spaceIndustry.setCreateTime(projectPark.getCreateTime());
        spaceIndustry.setSpaceId(projectPark.getSerialNumber());
        projectParkMapper.insertProjectPark(projectPark);
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        spaceIndustry.setParkId(String.valueOf(projectPark.getId()));
        return spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);
    }

    private static void extracted(ProjectPark projectPark, SpaceIndustry spaceIndustry) {
        spaceIndustry.setBuildType(1);
        spaceIndustry.setReleaseType(0L);//供给
        spaceIndustry.setApprovalState(projectPark.getApprovalState());
        spaceIndustry.setProvinceId(projectPark.getProvinceId());//省份
        spaceIndustry.setCityId(projectPark.getCityId());//城市
        spaceIndustry.setSupplyType(projectPark.getSupplyType());//服务类型
        spaceIndustry.setParkId(String.valueOf(projectPark.getId()));
        spaceIndustry.setHeating(projectPark.getParkHeating());//供暖
        spaceIndustry.setRefrig(projectPark.getParkRefrig());//制冷
        spaceIndustry.setPower(projectPark.getParkPower());//电力
        spaceIndustry.setGas(projectPark.getParkGas());//燃气
        spaceIndustry.setCoverResource(projectPark.getCoverResource());//封面资源
        spaceIndustry.setSpaceName(projectPark.getParkName());//园区名称
        spaceIndustry.setSpaceAllSize(projectPark.getParkAllSize());//空间总面积
        spaceIndustry.setTop(projectPark.getTop());//置顶
    }

    /**
     * 修改项目-园区
     *
     * @param projectPark 项目-园区
     * @return 结果
     */
    @Override
    public int updateProjectPark(ProjectPark projectPark)
    {
        projectPark.setApprovalState(0L);//未审核
        projectPark.setUpdateTime(DateUtils.getNowDate());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectPark,spaceIndustry);
        spaceIndustry.setSpaceId(String.valueOf(projectPark.getId()));
        spaceIndustry.setUpdateBy(SecurityUtils.getUsername());
        spaceIndustry.setUpdateTime(projectPark.getUpdateTime());
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        spaceIndustryMapper.updateSpaceIndustryByParkId(spaceIndustry);
        return projectParkMapper.updateProjectPark(projectPark);
    }

    /**
     * 批量删除项目-园区
     *
     * @param ids 需要删除的项目-园区主键
     * @return 结果
     */
    @Override
    public int deleteProjectParkByIds(Long[] ids)
    {
        return projectParkMapper.deleteProjectParkByIds(ids);
    }

    /**
     * 删除项目-园区信息
     *
     * @param id 项目-园区主键
     * @return 结果
     */
    @Override
    public int deleteProjectParkById(Long id)
    {
        spaceIndustryMapper.deleteSpaceIndustryByProjectParkId(id);
        return projectParkMapper.deleteProjectParkById(id);
    }

    @Override
    public int deleteProjectParkBySerialNumber(String spaceId) {
        spaceIndustryMapper.deleteSpaceIndustryBySerialNumber(spaceId);
        return projectParkMapper.deleteProjectParkBySerialNumber(spaceId);
    }
}
